package org.tc.controller;

import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.tc.bean.Item;
import org.tc.bean.ItemDesc;
import org.tc.common.Result;
import org.tc.common.ResultUtils;
import org.tc.result.EasyUIResult;
import org.tc.service.ItemDescService;
import org.tc.service.ItemService;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by zxd on 2018/1/26.
 */
@Controller
@RequestMapping("/item")
public class ItemController {

    @Autowired
    private ItemService itemService;

    @Autowired
    private ItemDescService itemDescService;


    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ResponseBody
    public Result saveItem(Item item, String desc){
        //调用service处理
        itemService.saveItem(item,desc);
        return ResultUtils.buildSuccess();
    }

    @RequestMapping(value = "/list")
    @ResponseBody
    public EasyUIResult list(@RequestParam(value = "page",defaultValue = "1")Integer page,
                             @RequestParam(value = "rows",defaultValue = "10")Integer rows){
        //分页查询结果
        PageInfo<Item> pageInfo = itemService.queryPageListByWhere(page, rows, null);
        //组装easyUI显示需要的数据
        EasyUIResult easyUIResult=new EasyUIResult(pageInfo.getTotal(),pageInfo.getList());
        return easyUIResult;
    }

    @RequestMapping(value = "/desc/{itemId}")
    public ResponseEntity<ItemDesc> desc(@PathVariable("itemId")Long itemId){
        return ResponseEntity.ok(itemDescService.queryById(itemId));
    }



    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @ResponseBody
    public Result updateItem(Item item,String desc){
        //调用service处理
        itemService.updateItem(item,desc);
        return ResultUtils.buildSuccess();
    }


}
